<?php

/**
 * @author Lim Yi Le
 */
class shelflocator extends CI_Controller{

/*
|--------------------------------------------------------------------------
| getDirectoryImageFilenames (@author : Lim Yi Le)
|--------------------------------------------------------------------------
|
| This function scans the shelflocator image directory and returns the image filename 
| results as JSON and call the function to wrap the response in the requested callback. 
|
*/   
    function getDirectoryImageFilenames() {
        
        $imageFilenames = scandir("./resources/images/shelflocator/", 1);
        $imageFilenamesJsonPFormat = array();
        
        for($i = 0; $i < count($imageFilenames); $i++){
            $imageFilenamesJsonPFormat[] = array('image' => (string)$imageFilenames[$i]);
        }
        $output = array('shelfLocator' => $imageFilenamesJsonPFormat);
        
        $this->retJsonP($output); 
    }
/*
|--------------------------------------------------------------------------
| retJsonP (@author : Justina Lee)
|--------------------------------------------------------------------------
|
| This function wraps the response in the requested callback. 
|
*/
    
    function retJsonP($output) {
        $callback = $_REQUEST['callback'];

        if ($callback) {
            header('Content-Type: text/javascript');
            echo $callback . '(' . json_encode($output) . ');';
        } else {
            header('Content-Type: application/x-json');
            echo json_encode($output);
        }
    }
}

?>
